package com.link2room.aeriabots.controller.manager;

import com.link2room.aeriabots.dto.param.api.WxReplyTemplateApiParamDto;
import com.link2room.aeriabots.dto.result.base.WxReplyTemplateDto;
import com.link2room.aeriabots.facade.IWXReplyFacadeService;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.aeriabots.constant.PermissionConstant.*;

/**
 * 微信关键字回复
 * Created by LN on 2016/12/1.
 */
@Api(tags = "微信公众号回复模板")
@RestController
@RequestMapping("/manager/ReplyTemplates")
public class WxReplyTemplateController {
    @Resource
    private IWXReplyFacadeService wxReplyFacadeService;

    @ApiOperation(value = "微信公众号回复模板列表", response = WxReplyTemplateDto.class)
    @ApiImplicitParams(value = {
    })
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = wxReplyTemplates, name = "微信关键字回复模板")
    public @ResponseBody
    BaseResultDto getTemplate(
    ) throws Exception {
        WxReplyTemplateApiParamDto paramDto = new WxReplyTemplateApiParamDto();
        QueryResult<WxReplyTemplateDto> resDto = wxReplyFacadeService.getWxReplyTemplates(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setQueryResult(resDto);
        return resultDto;
    }

    @ApiOperation(value = "微信公众号回复模板详情", response = WxReplyTemplateDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "templateName", value = "模板名称", required = false,paramType = "query"),
            @ApiImplicitParam(name = "templateId", value = "模板id", required = false,paramType = "query"),
            @ApiImplicitParam(name = "isHalt", value = "是否禁用", required = false,paramType = "query"),
    })
    @RequestMapping(value = "/Details", method = RequestMethod.GET)
    @L2RPermissionResources(value = wxReplyTemplatesDetails, name = "微信关键字回复模板详情")
    public @ResponseBody
    BaseResultDto getTemplateDetail(
            @RequestParam(name = "templateName", required = false) String templateName,
            @RequestParam(name = "templateId", required = false) String templateId,
            @RequestParam(name = "isHalt", required = false) String isHalt
    ) throws Exception {
        WxReplyTemplateApiParamDto paramDto = new WxReplyTemplateApiParamDto();

        paramDto.setTemplateName(templateName);
        paramDto.setTemplateId(templateId);
        paramDto.setIsHalt(isHalt);
        QueryResult<WxReplyTemplateDto> resDto = wxReplyFacadeService.getWxReplyTemplateDetails(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setQueryResult(resDto);
        return resultDto;
    }

    @ApiOperation(value = "保存微信公众号回复模板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "paramDto", value = "模板", required = false,dataTypeClass = WxReplyTemplateApiParamDto.class,paramType = "body"),
    })
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = saveWxReplies, name = "保存微信回复")
    public @ResponseBody
    BaseResultDto saveReplies(
            @RequestBody WxReplyTemplateApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.createWxReplyTemplate(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

    @ApiOperation(value = "更新微信公众号回复模板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "paramDto", value = "模板", required = false,dataTypeClass = WxReplyTemplateApiParamDto.class,paramType = "body"),
    })
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateWxReplies, name = "更新微信回复模板")
    public @ResponseBody
    BaseResultDto updateReplies(
            @RequestBody WxReplyTemplateApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.updateReplyTamplate(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }


    @RequestMapping(method = RequestMethod.DELETE)
    @Deprecated
    @L2RPermissionResources(value = deleteWxReplies, name = "删除微信回复模板")
    public @ResponseBody
    BaseResultDto delReply(
            @RequestBody WxReplyTemplateApiParamDto paramDto
    ) throws Exception {
        wxReplyFacadeService.delReplyTemplate(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }
    @ApiOperation(value = "删除微信公众号回复模板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "templateId", value = "模板id", required = true,paramType = "path"),
    })
    @RequestMapping(value = "/{templateId}",method = RequestMethod.DELETE)
    @L2RPermissionResources(value = deleteWxRepliesNew, name="删除微信公众号回复模板")
    public @ResponseBody
    BaseResultDto delReply(
            @PathVariable("templateId") String templateId
    ) throws Exception {
        WxReplyTemplateApiParamDto paramDto = new WxReplyTemplateApiParamDto();
        paramDto.setTemplateId(templateId);
        wxReplyFacadeService.delReplyTemplate(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

}
